using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace Times
{
    public partial class Options : Form
    {
        private DesktopForm frm;

        public Options(DesktopForm frm)
        {
            this.frm = frm;
            InitializeComponent();
            Assembly asm = Assembly.GetExecutingAssembly();
            AssemblyName an = asm.GetName();
            this.lblVersion.Text = "Version: " + an.Version;  
          }

        private void button1_Click(object sender, EventArgs e)
        {
			Properties.Settings.Default.Latitude = double.Parse(tbLatitude.Text);
			Properties.Settings.Default.Longitude = double.Parse(tbLong.Text);
			if (cbTimeZones.SelectedIndex >= 0)
				Properties.Settings.Default.TimeZone = timeZones[cbTimeZones.SelectedIndex].BaseUtcOffset.Hours;

			Properties.Settings.Default.Madhab = cbMadthab.Items[cbMadthab.SelectedIndex].ToString();
			Properties.Settings.Default.CalculationMethod = cbCalculationMethod.Items[cbCalculationMethod.SelectedIndex].ToString();

            frm.RefreshData();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.colorDialog1.ShowDialog(this) == DialogResult.OK)
            {
                Properties.Settings.Default.WarnColor = colorDialog1.Color;
                button2.ForeColor = colorDialog1.Color;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (this.colorDialog1.ShowDialog(this) == DialogResult.OK)
            {
                Properties.Settings.Default.AlertColor = colorDialog1.Color;
                button3.ForeColor = colorDialog1.Color;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (this.colorDialog1.ShowDialog(this) == DialogResult.OK)
            {
                Properties.Settings.Default.NormColor = colorDialog1.Color;
                button4.ForeColor = colorDialog1.Color;
            }
        }


        private void button7_Click(object sender, EventArgs e)
        {
            if (this.colorDialog1.ShowDialog(this) == DialogResult.OK)
            {
                Properties.Settings.Default.BackColor = colorDialog1.Color;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Close();
        }

		private void Options_Load(object sender, EventArgs e)
		{
			tbLatitude.Text = Properties.Settings.Default.Latitude.ToString();
			tbLong.Text = Properties.Settings.Default.Longitude.ToString();
			
			cbIsDST.Checked = TimeZoneInfo.Local.IsDaylightSavingTime(DateTime.Now);
			loadTimeZones();
			loadCalculationMethods();
		}

		private List<TimeZoneInfo> timeZones = new List<TimeZoneInfo>();

		private void loadCalculationMethods()
		{
			foreach (string name in Enum.GetNames(typeof(CalculationMethods)))
			{
				int i = cbCalculationMethod.Items.Add(name);
				if (Properties.Settings.Default.CalculationMethod == name)
					cbCalculationMethod.SelectedIndex = i;
			}

			foreach (string name in Enum.GetNames(typeof(JurusticMethods)))
			{
				int i = cbMadthab.Items.Add(name);
				if (Properties.Settings.Default.Madhab == name)
					cbMadthab.SelectedIndex = i;
			}
		}

		private void loadTimeZones()
		{
			foreach (TimeZoneInfo zone in TimeZoneInfo.GetSystemTimeZones())
			{
				timeZones.Add(zone);
				int index = cbTimeZones.Items.Add(zone.DisplayName);
				
				if (zone.DisplayName == TimeZoneInfo.Local.DisplayName)
					cbTimeZones.SelectedIndex = index;
			}
		}

		private void btnFind_Click(object sender, EventArgs e)
		{
			AddressLookupForm al = new AddressLookupForm();
			if (al.ShowDialog() == DialogResult.OK)
			{
				tbLatitude.Text = al.Latitude;
				tbLong.Text = al.Longitude;
			}
		}
    }
}